package com.scrx.ymg.util;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import com.scrx.ymg.dao.orderform.IOrderFormDao;
import com.scrx.ymg.entity.OrderForm;
import com.scrx.ymg.service.orderform.IOrderFormService;
import com.scrx.ymg.service.seckill.ISeckillService;
import com.scrx.ymg.utilqy.HttpUtils;

/**
 * 定时执行计划任务
 * @author Administrator
 *
 */
public class TimedTaskUtil {
	@Autowired
	private IOrderFormDao orderFormDao;
	@Autowired
	private IOrderFormService  ofs;
	@Autowired
	private ISeckillService kills;
	
	
	/**
	 * 每小时固定更新access_token
	 */
	public void updateAccessToken(){
		//System.out.println("APPID"+PayConfigUtil.APPID+"SECRET"+PayConfigUtil.SECRET);
		String tempUrl=PayConfigUtil.getTokenUEL+"&appid="+PayConfigUtil.APPID+"&secret="+PayConfigUtil.SECRET;
		PayConfigUtil.access_token=BaseUtil.getJsonString(HttpUtils.get(tempUrl), "access_token");
		System.out.println("JBT：每小时固定更新access_token"+PayConfigUtil.access_token);
	}
	
	
    /**
     * 直营订单每天凌晨自动调用订单收货功能
     * @param day 自动收货订单天数 默认7天后自动收货
     */
	public void superAutomaticReceiving(){
		
		OrderForm order=new OrderForm();
		order.setStatus(2);//待收货
		order.setTypeId(3);//直营
		order.setOrderFormTime(getDateTime(-7));//7天前的当前时间
		List<OrderForm> list=orderFormDao.getOrderFormList(order);
		for (OrderForm orderForm : list) {
			ofs.directTakeDeliveryYongJin(orderForm.getId());
		}
	}

	public String getDateTime(int data){
		Date date = new Date();
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.DATE, data);
		date = ca.getTime();
		SimpleDateFormat  sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
		String dateTime=sdf.format(date);
		return dateTime;
	}
	public String getDate(int data){
		Date date = new Date();
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.DATE, data);
		date = ca.getTime();
		SimpleDateFormat  sdf = new SimpleDateFormat ("yyyy-MM-dd");
		String dateTime=sdf.format(date);
		return dateTime;
	}
}
